package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.data.ActivityInListData;
import com.wbao.dianniu.im.LoginSampleHelper;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.logical.GlobalUserData;
import com.wbao.dianniu.manager.ActInConfirmManger;
import com.wbao.dianniu.manager.ActivityHelperManager;
import com.wbao.dianniu.utils.PermissionUtils;
import com.wbao.dianniu.utils.PictureDownloadUtils;

/**
 * 已加入活动列表
 * 
 * @author yejianjun
 *
 */
public class ActivityInListAdapter extends BaseAdapter {
	private Context mContext;
	private List<ActivityInListData> list;
	private LayoutInflater mInflater;
	private ActInConfirmManger manager;
	public ActivityInListAdapter(Context context) {
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		manager = new ActInConfirmManger(mContext);
		if(null == list)
			list = new ArrayList<ActivityInListData>();
	}
//	public void setData(List<ActivityInListData> list){
//		this.list = list;
//	}
	public List<ActivityInListData> getList() {
		return list;
	}
	@Override
	public int getCount() {
		if (null == list)
			return 0;
		else
			return list.size();
	}
	public void clear(){
		if (null != list) {
			list.clear();
		}
	}
	public void addData(List<ActivityInListData> addList){
		for (int i = 0; i < addList.size(); i++) {
			list.add(addList.get(i));
		}
	}
	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.already_join_item, null);   
			holder = new Holder();
			holder.head = (ImageView)convertView.findViewById(R.id.already_join_imageview);
			holder.name =  (TextView)convertView.findViewById(R.id.already_join_name);
			holder.label = (TextView) convertView.findViewById(R.id.already_join_label);
			holder.agreedBtn = (Button) convertView.findViewById(R.id.already_agreed);
			holder.refuseBtn = (Button) convertView.findViewById(R.id.already_refuse);
			holder.alreadyBtn = (Button) convertView.findViewById(R.id.already_process_btn);
			holder.callTV = (TextView)convertView.findViewById(R.id.already_call);
			holder.msnTV =  (TextView)convertView.findViewById(R.id.already_send_msn);
			holder.msgTV = (TextView) convertView.findViewById(R.id.already_send_msg);
			convertView.setTag(holder); 
		}else{
			 holder = (Holder) convertView.getTag();   
		}
		final ActivityInListData data = list.get(position);
		PictureDownloadUtils.displayHeadImage(mContext,holder.head, data.getHeadPic());
		holder.name.setText(data.getRealName());
		holder.label.setText(data.getLabel() == null ? "" : data.getLabel());
		if(1 == data.getStatus()){
			holder.alreadyBtn.setVisibility(View.GONE);
			holder.agreedBtn.setVisibility(View.VISIBLE);
			holder.refuseBtn.setVisibility(View.VISIBLE);
		}else if(2 == data.getStatus()){
			holder.alreadyBtn.setVisibility(View.VISIBLE);
			holder.agreedBtn.setVisibility(View.GONE);
			holder.refuseBtn.setVisibility(View.GONE);
			holder.alreadyBtn.setText(mContext.getResources().getString(R.string.already_agreed));
		}else if(3 == data.getStatus()){
			holder.alreadyBtn.setVisibility(View.VISIBLE);
			holder.agreedBtn.setVisibility(View.GONE);
			holder.refuseBtn.setVisibility(View.GONE);
			holder.alreadyBtn.setText(mContext.getResources().getString(R.string.already_refused));
		}
		holder.agreedBtn.setOnClickListener(new MyClickListener(data.getAccountId(),data.getActivityId(),holder.alreadyBtn,holder.agreedBtn,holder.refuseBtn));
		holder.refuseBtn.setOnClickListener(new MyClickListener(data.getAccountId(),data.getActivityId(),holder.alreadyBtn,holder.agreedBtn,holder.refuseBtn));
		holder.callTV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
				builder.setTitle("确认拨打电话：" + data.getMobile());
				builder.setPositiveButton(mContext.getResources().getString(R.string.confirm),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
								//调用拨打电话
								PermissionUtils.getInstance().intentToCall(mContext,data.getMobile());
							}
						});
				builder.setNegativeButton(mContext.getResources().getString(R.string.cancel),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});
				builder.create().show();
			}
		});
		holder.msnTV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 发短信
				/*Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + data.getMobile()));
				intent.putExtra("sms_body", "");
				mContext.startActivity(intent);*/
				PermissionUtils.getInstance().intentToSendSMS(mContext, data.getMobile(), "");
			}
		});
		holder.msgTV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//跳转到聊天界面
				String appkey = mContext.getResources().getString(R.string.app_key);
				GlobalUserData userData = new GlobalUserData();
				LoginSampleHelper.getInstance().openChat(mContext,userData.getEnvironemt() + data.getAccountId()+"", appkey);
			}
		});
		return convertView;
	}

	class Holder{
		public ImageView head;
		public TextView name;
		public TextView label;
		public Button agreedBtn;
		public Button refuseBtn;
		public Button alreadyBtn;
		public TextView callTV;
		public TextView msnTV;
		public TextView msgTV;
	}
	
	class MyClickListener implements OnClickListener{

		private int accountId;
		private int activityId;
		private Button alreadyBtn;
		private Button agreedBtn;
		private Button refuseBtn;
		public MyClickListener(int accountId,int activityId,Button already,Button agreed,Button refuse) {
			this.accountId = accountId;
			this.activityId = activityId;
			alreadyBtn = already;
			agreedBtn = agreed;
			refuseBtn = refuse;
		}
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.already_agreed:	// 同意
				manager.inconfirm(accountId, activityId, 2);
				alreadyBtn.setVisibility(View.VISIBLE);
				agreedBtn.setVisibility(View.GONE);
				refuseBtn.setVisibility(View.GONE);
				alreadyBtn.setText(mContext.getResources().getString(R.string.already_agreed));
				ActivityHelperManager.getInstance().IsChanged(activityId, accountId, 4);
				break;
			case R.id.already_refuse:	//拒绝
				manager.inconfirm(accountId, activityId, 3);
				alreadyBtn.setVisibility(View.VISIBLE);
				agreedBtn.setVisibility(View.GONE);
				refuseBtn.setVisibility(View.GONE);
				alreadyBtn.setText(mContext.getResources().getString(R.string.already_refused));
				ActivityHelperManager.getInstance().IsChanged(activityId, accountId, 4);
				break;
			default:
				break;
			}
		}
		
	}
	
}
